package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.*;
import com.qf.pojo.*;
import com.qf.service.DtsGoodsService;
import com.qf.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @auther Administrator
 * @date 2023/2/23 8:11
 */
@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {

    @Autowired
    private DtsGoodsMapper dtsGoodsMapper;
    @Autowired
    private DtsGoodsSpecificationMapper dtsGoodsSpecificationMapper;
    @Autowired
    private DtsGoodsAttributeMapper dtsGoodsAttributeMapper;
    @Autowired
    private DtsGoodsProductMapper dtsGoodsProductMapper;
    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;
    @Autowired
    private DtsOrderGoodsMapper dtsOrderGoodsMapper;
    @Autowired
    private DtsCartMapper dtsCartMapper;

    @Override
    public Long findCount() {
        QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);
        return dtsGoodsMapper.selectCount(queryWrapper);

    }

    @Override
    public IPage<DtsGoods> findAllByPage(Integer page, Integer limit, String name, String goodsSn,
                                         String sort, String order, List<Integer> brandIds) {
        return dtsGoodsMapper.selectPage(new Page<>(page,limit),
                new QueryWrapper<DtsGoods>().
                        like(!StringUtils.isEmpty(name),"name",name).
                        like(!StringUtils.isEmpty(goodsSn),"goods_sn",goodsSn).
                        in(!CollectionUtils.isEmpty(brandIds),"brand_id",brandIds).
                        orderBy(StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort));
    }

    /**     添加
     *     *  private DtsGoods goods; 商品信息
     *      * 	private DtsGoodsSpecification[] specifications;   商品规格
     *      * 	private DtsGoodsAttribute[] attributes;         商品参数
     *      * 	// 这里采用 Product 再转换到 DtsGoodsProduct
     *      * 	private DtsGoodsProduct[] products;         商品货品
     * @param goodsAllinone
     */
    @Transactional
    @Override
    public void create(GoodsAllinone goodsAllinone) {
        //获取对象
        DtsGoods goods = goodsAllinone.getGoods();
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        DtsGoodsProduct[] products = goodsAllinone.getProducts();

        //判断商品库是否存在
//        DtsGoods name = dtsGoodsMapper.selectOne(new QueryWrapper<DtsGoods>().eq("name", goods.getName()));
//        if (name == null) {
//            throw new RuntimeException("商品已经存在");
//        }
        //添加商品信息
        dtsGoodsMapper.insert(goods);
        //添加商品规格信息
        for (DtsGoodsSpecification specification : specifications) {
            dtsGoodsSpecificationMapper.insert(specification);
        }
        //添加商品参数
        for (DtsGoodsAttribute attribute : attributes) {
            dtsGoodsAttributeMapper.insert(attribute);
        }
        //添加货品
        for (DtsGoodsProduct product : products) {
            dtsGoodsProductMapper.insert(product);
        }
    }
    //跳转修改页面的回调数据
    @Transactional
    @Override
    public Map detail(Integer id) {
        //查询商品
        DtsGoods dtsGoods = dtsGoodsMapper.selectById(id);
        //根据商品id查询各项信息
        List<DtsGoodsAttribute> attributes = dtsGoodsAttributeMapper.selectList(new QueryWrapper<DtsGoodsAttribute>().eq("goods_id", id));
        List<DtsGoodsProduct> products = dtsGoodsProductMapper.selectList(new QueryWrapper<DtsGoodsProduct>().eq("goods_id", id));
        List<DtsGoodsSpecification> specifications = dtsGoodsSpecificationMapper.selectList(new QueryWrapper<DtsGoodsSpecification>().eq("goods_id", id));
        //根据商品查询
        Integer categoryId = dtsGoods.getCategoryId();
        DtsCategory category = dtsCategoryMapper.selectById(categoryId);
        Integer[] categoryIds =null;
        if (category != null) {
            categoryIds = new Integer[] {category.getPid(),category.getId()};
        }


        Map data = new HashMap();
        data.put("categoryIds",categoryIds);
        data.put("goods", dtsGoods);
        data.put("attributes",attributes);
        data.put("products",products);
        data.put("specifications",specifications);

        return data;
    }

    /**
     * 确认修改
     * @param goodsAllinone
     */
    @Transactional
    @Override
    public void update(GoodsAllinone goodsAllinone) {
        //获取对象
        DtsGoods goods = goodsAllinone.getGoods();
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        // 检查是否存在购物车商品或者订单商品。
        List<DtsOrderGoods> orderGoodsList = dtsOrderGoodsMapper.selectList(new QueryWrapper<DtsOrderGoods>().eq("goods_id", goods.getId()));
        List<DtsCart> cartList = dtsCartMapper.selectList(new QueryWrapper<DtsCart>().eq("goods_id", goods.getId()));
        if(!CollectionUtils.isEmpty(orderGoodsList)||!CollectionUtils.isEmpty(cartList)){
            throw new RuntimeException("该商品还存在于用户订单或购物车");
        }
        //修改商品表
        goods.setUpdateTime(new Date());
        dtsGoodsMapper.updateById(goods);
        //删除其他附属表中该商品的信息
        dtsGoodsAttributeMapper.delete(new QueryWrapper<DtsGoodsAttribute>().eq("goods_id", goods.getId()));
        dtsGoodsProductMapper.delete(new QueryWrapper<DtsGoodsProduct>().eq("goods_id", goods.getId()));
        dtsGoodsSpecificationMapper.delete(new QueryWrapper<DtsGoodsSpecification>().eq("goods_id", goods.getId()));
        //添加
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(new Date());
            dtsGoodsAttributeMapper.insert(attribute);
        }
        for (DtsGoodsSpecification specification : specifications) {
            specification.setUpdateTime(new Date());
            dtsGoodsSpecificationMapper.insert(specification);
        }
        for (DtsGoodsProduct product : products) {
            product.setUpdateTime(new Date());
            dtsGoodsProductMapper.insert(product);
        }
    }

    /**
     * 删除
     * @param goods
     */
    @Override
    public void delete(DtsGoods goods) {
        dtsGoodsMapper.deleteById(goods);
        dtsGoodsAttributeMapper.delete(new QueryWrapper<DtsGoodsAttribute>().eq("goods_id", goods.getId()));
        dtsGoodsProductMapper.delete(new QueryWrapper<DtsGoodsProduct>().eq("goods_id", goods.getId()));
        dtsGoodsSpecificationMapper.delete(new QueryWrapper<DtsGoodsSpecification>().eq("goods_id", goods.getId()));
    }

    @Override
    public DtsGoods findGoodsById(Long goodsId) {
        return dtsGoodsMapper.selectById(goodsId);
    }

}
